package test;


import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SendImgDemo {
    public static void main(String[] args) throws Exception{
//        String strKey = "RTSz9xq1DhCGI82ROC+rZ0U0s/catQ4Q";   //密钥
//        String shl_id="45000001"; //学校编号
//        String ECARDEVID="101024"; //一卡通编号
//        String head_url = "https://app.xiaoyuan.ccb.com/LHECISM/CCBYXServiceReqServlet?ECARDEVID="+ECARDEVID+"&SCHID="+shl_id+"&ccbParam=";
//        //参数加密
//        String ccbParm = YXY966();
//        MCipherEncryptor ccbEncryptor = new MCipherEncryptor(strKey.substring(strKey.length() - 30));
//        ccbParm = ccbEncryptor.doEncrypt(ccbParm);
//        System.out.println(head_url+ccbParm);

        //将图片转为base64编码
        String base64Strimg = imageToBase64Str("./imgs/h1.jpg");
        System.out.println(base64Strimg);
//        System.out.println(base64Strimg.length());
    }

    public static String YXY966(){
        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        System.out.println("YXY966");
        String TXCODE = "YXY966";
        String ST_ID   = "18651506879";//学工号
        String shl_id   = "45000001";//学校编号
        String TERMNO = "202400081420";//终端ID
        String ORDERID = "TESTYXY966"+date;//订单号
        //将图片转为base64编码
        String base64Strimg = imageToBase64Str("./imgs/h1.jpg");

        String ccbParm ="TXCODE="+TXCODE+"&BRANCHID=450000000"+"&SHL_ID="+shl_id+"&ST_ID="+ST_ID+
                "&TERMNO="+TERMNO+"&ORDERID="+ORDERID+"&ORDER_IMAGE="+base64Strimg+"&ORDER_TIME="+date;
        return ccbParm;
    }
    /**
     * 将图片转为Base64编码
     * @param imgpath
     * @return
     */
    public static String imageToBase64Str(String imgpath) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgpath);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            // BASE64编码
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("将图片转换为Base64编码异常");
            return "异常";
        }
    }
}
